import os
import sys
import subprocess
import zipfile

from subprocess import call
if len(sys.argv) < 2:
    print >> sys.stderr, "Usage: %s apk" % sys.argv[0]
    sys.exit(1)


apk_file = sys.argv[1]
apk_file = os.path.expanduser(apk_file)
tmp_ras_file = os.path.expanduser('tmp_rsa')

zip = zipfile.ZipFile(apk_file)
for name in zip.namelist():
    if name.startswith('META-INF') and name.endswith("RSA"):
	print name
	with zip.open(name) as f_in:
            with open(tmp_ras_file, 'w') as f_out:
                for line in f_in:
                    f_out.write(line)
        break

# cmd = 'keytool -list -printcert -jarfile %s' % (apk_file)
cmd = 'keytool -printcert -file %s' % (tmp_ras_file)
print cmd
print '---------\n'
cmd = cmd.split()

output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
print output
print '---------\n'
print output.replace(':', '').lower()

os.remove(tmp_ras_file)
